//
//  StatusCell.swift
//  SinaMicroblog
//
//  Created by 唐绍禹 on 15/8/6.
//  Copyright © 2015年 唐绍禹. All rights reserved.
//

import UIKit

let statusCellControlMargin: CGFloat = 8

enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    static func cellID(status: Status) -> String {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatusCell: UITableViewCell {

    var status: Status? {
        didSet{
            topView.status = status
            contentLabel.text = status?.text ?? ""
            
            pictureView.status = status
            
            pictureHeightCons?.constant = pictureView.bounds.size.height
            pictureWithCons?.constant = pictureView.bounds.size.width
            pictureTopCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : statusCellControlMargin
        }
    }
    /// 图片宽度约束
    var pictureWithCons: NSLayoutConstraint?
    /// 图片高度约束
    var pictureHeightCons: NSLayoutConstraint?
    /// 图片顶部约束
    var pictureTopCons: NSLayoutConstraint?
    //计算行高
    func rowHeight(status:Status) -> CGFloat {
        self.status = status
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        contentView.addSubview(contentLabel)
        //顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        //指定contentLabel宽度和底部约束
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: 2 * statusCellControlMargin))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellControlMargin))
        
        //底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 30), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
        
        //不用自动计算行高就不需要设置底部约束
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
    
    private lazy var topView: StatusTopView = StatusTopView()
    lazy var bottomView: StatusBottomView = StatusBottomView()
    lazy var pictureView: StatusPictureView = StatusPictureView()
    lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.blackColor(), fontSize: 15)
        label.numberOfLines = 0
        
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellControlMargin
        return label
    }()
}
